
clear
use "${source_data}\FINAL_MATCHED.dta"

global isid_vars date UD_num HS6_main custom_office registration_nber itm_nber 

* Classifying outputs:
capture drop woven
capture drop knit
do "${code}\Classifying Outputs.do"
gen selected_p=( (type_of_product==7 | type_of_product==4) & woven==1) 

* Recoding updated buyer codes:
replace b_id=72 if b_id==25 | b_id==3130
replace b_id=1777 if b_id==7054
replace b_id=380 if b_id==3753
replace b_id=1114 if b_id==5409
replace b_id=1716 if b_id==2854
replace b_id=1746 if b_id==1747
replace b_id=1736 if b_id==2869 
replace b_id=1365  if b_id==1366
decode b_id, gen(b_string)
replace b_id=1988 if regexm(b_string,"INDITEX")==1
replace b_id=1988 if regexm(b_string,"ITX TRADING")==1
replace b_id=1988 if regexm(b_string,"BERSHKA")==1
replace b_id=1988 if regexm(b_string,"STRADIVARIOUS")==1
replace b_id=1988 if regexm(b_string,"PULL & BEAR")==1
drop b_string

* ---------------------------------------------------------------------------- *
* MERGE IN WORKING SAMPLE 
* ---------------------------------------------------------------------------- *

drop if custom_office==.
drop if registration_nber==.
drop if itm_nber==.
drop if date==.

sort custom_office registration_nber itm_nber date
merge 1:1 custom_office registration_nber itm_nber date using "${replicated_data}\In_Sample_Observations.dta" 

replace S_pds=0 if S_pds==. 

* ---------------------------------------------------------------------------- *
* UDs AND BOND PROCEDURES 
* ---------------------------------------------------------------------------- *

capture drop no_UDs
gen no_UDs=(UD_num==. | UDs_mer==1)

tab selected_p no_UDs
tab exp_extd no_UDs if selected_p==1

capture drop _merge 
sort custom_office registration_nber itm_nber date
merge 1:1 custom_office registration_nber itm_nber date using "${source_data}\In_Sample_Outliers.dta" 

* ---------------------------------------------------------------------------- *
* COVERAGE (for text in Appendix)
* ---------------------------------------------------------------------------- *

tab woven 

capture drop total_q*
capture drop total_pq*
local V q pq
foreach v of local V{
egen total_`v'_g=total(`v'_exp)
egen total_`v'_w=total(`v'_exp*(woven==1))
egen total_`v'_w4=total(`v'_exp*(HS4=="6203" | HS4=="6204" | HS4=="6205" | HS4=="6206"))
egen total_`v'_p=total(`v'_exp*(S_p==1))
egen total_`v'_ud=total(`v'_exp*(selected_p==1 & no_UDs==0))
egen total_`v'_udd=total(`v'_exp*(S_pd==1))
egen total_`v'_as=total(`v'_exp*(S_pds==1))
egen total_`v'_ep=total(`v'_exp*(exp_extd_procedure==1072 | exp_extd_procedure==1071 )*(selected_p==1))
egen total_`v'_udall=total(`v'_exp*(no_UDs==0))
}


capture drop sh_w_g sh_w4_g sh_w4_w sh_p_w sh_p_w4  sh_as_udd sh_udd_ud sh_udd_w4 sh_udd_p sh_udall_g
gen sh_udall_g=total_q_udall/total_q_g
gen sh_w_g=total_q_w/total_q_g
gen sh_w4_g=total_q_w4/total_q_g
gen sh_w4_w=total_q_w4/total_q_w
gen sh_p_w=total_q_p/total_q_w
gen sh_p_w4=total_q_p/total_q_w4
gen sh_ud_w4=total_q_ud/total_q_w4 
gen sh_udd_w4=total_q_udd/total_q_w4
gen sh_udd_p=total_q_udd/total_q_p
gen sh_udd_ud=total_q_udd/total_q_ud
gen sh_as_p=total_q_as/total_q_p 
gen sh_ud_w=total_q_ud/total_q_w 
gen sh_ud_p=total_q_ud/total_q_p 
gen sh_as_ud=total_q_as/total_q_ud
gen sh_as_udd=total_q_as/total_q_udd
gen sh_as_w=total_q_as/total_q_w
gen sh_as_ep=total_q_as/total_q_ep

* ---------------------------------------------------------------------------- *
* TABLE B1. Comparisons of Volumes and Prices Across Samples
* ---------------------------------------------------------------------------- *

do "${floats}\TableB1PanelA.do"
do "${floats}\TableB1PanelB.do"

* ---------------------------------------------------------------------------- *
* SAMPLES FOR ROBUSTNESS
* ---------------------------------------------------------------------------- *
capture drop _merge
sort b_id
merge m:1 b_id using  "${replicated_data}\Sample_b_PD.dta"

gen in_PD_sample=(_merge==3)

gen SAMPLE_0=1
gen SAMPLE_1=(woven==1)
gen SAMPLE_2=(selected_p==1)
gen SAMPLE_3=(selected_p==1 & no_UDs==0)
gen SAMPLE_4=(selected_p==1 & no_UDs==0 & S_pd==1)
gen SAMPLE_5=(S_pds==1) 
gen SAMPLE_6=(S_pds==1 & in_PD_sample==1)


capture drop _merge
sort b_id
merge m:1 b_id using  "${source_data}\Buyer_Level_Data.dta"
drop if b_id==1


* ---------------------------------------------------------------------------- *
* RELATIONAL SOURCING INSIDE THE DATA 
* ---------------------------------------------------------------------------- *

sort custom_office registration_nber date itm_nber
egen gr_isid=group(custom_office registration_nber date itm_nber)

capture drop num_l_bjt
bys b_id HS6 year: egen num_l_bjt=nvals(gr_isid)
capture drop yy
bys b_id HS6 year: egen yy=mean(num_l_bjt)
replace num_l_bjt=yy if num_l_bjt==. 

capture drop sel
bys b_id HS6 year: egen sel=nvals(s_id)
capture drop yy
bys b_id HS6 year: egen yy=mean(sel)
replace sel=yy if sel==. 

capture drop sourcing_metric_bjt
gen sourcing_metric_bjt=-(sel/num_l_bjt)

capture drop xx 
capture drop XX
bys b_id HS6 year: egen xx=total(q_exp)
capture drop XX
bys b_id: egen XX=total(q_exp)
gen weight = xx/XX

capture drop w_sourcing_metric_bjt
gen w_sourcing_metric_bjt=weight*sourcing_metric_bjt
bys b_id HS6 year: gen n_bjt=_n
replace w_sourcing_metric_bjt=0 if n_bjt!=1

bys b_id: egen ALL_sourcing_metric_b=total(w_sourcing_metric_bjt)
label var ALL_sourcing_metric_b "\$Sel-Ship_b\$"

* Metric in excluded products: sourcing_metric_b_excl
* Metric in all products: ALL_sourcing_metric_b

* ---------------------------------------------------------------------------- *
* BUYER-, RELATIONSHIP- AND ORDER-LEVEL CONTROLS
* ---------------------------------------------------------------------------- *
* Construction replicates procedure in Assembly.do

bys b_id s_id: egen q_sb_all=total(q_exp) 
gen ln_q_sb_all=ln(q_sb_all) 
label var ln_q_sb_all "\$q_{sb}\$" 

bys b_id: egen q_b_all=total(q_exp) 
gen ln_q_b_all=ln(q_b_all) 
label var ln_q_b_all "\$q_{b}\$" 

bys b_id: egen QB=total(q_exp)
bys s_id: egen QS=total(q_exp)
bys b_id s_id: egen QSB=total(q_exp)
gen sh_s_b=QSB/QB
label var sh_s_b "\$Share^s_{b} \$" 
gen sh_b_s=QSB/QS
label var sh_b_s "\$Share^b_{s} \$" 

bys b_id: egen birth_b=min(date)
bys b_id s_id: egen birth_bs=min(date)

capture drop cohort_b
capture drop cohort_sb
gen cohort_b=yofd(birth_b )
gen cohort_sb=yofd(birth_bs)

sort b_id s_id UD_num date
bys UD_num: egen date_of_order=min(date)
replace date_of_order=date if date_of_order==.

gen age_buy_months=date_of_order-birth_b
replace age_buy_months=age_buy_months/30
replace age_buy_months=ln(age_buy_months)
label var age_buy_months "\$Age_{b} \$"

gen age_buysel_months=date_of_order-birth_bs
replace age_buysel_months=age_buysel_months/30
replace age_buysel_months=ln(age_buysel_months)
label var age_buysel_months "\$Age_{sb} \$"
 
replace signatory=0 if signatory==.
 
capture drop xx 
bys b_id destination: egen xx=total(q_exp)  
capture drop yy
by b_id: egen yy=rank(-xx),unique
capture drop zz
clonevar zz=destination if yy==1
capture drop top_dest_b
gen top_dest_b=zz
capture drop yy
bys b_id: egen yy=median(top_dest_b)
replace top_dest_b=yy 
* Regressions from here are at the transaction level (destination defined at the transaction level)
 
egen gr_sjt=group(s_id HS6_main year)

gen ln_q_tr=ln(q_exp)
gen ln_p_tr=ln(pq_exp/q_exp)
 
* ---------------------------------------------------------------------------- *
* TRANSACTION-LEVEL REGRESSIONS ON PRICES
* ---------------------------------------------------------------------------- *

capture drop n_b 
bys b_id: gen n_b=_n

cap drop SAMPLE_0 SAMPLE_1 SAMPLE_2 SAMPLE_3 SAMPLE_4 SAMPLE_5
gen SAMPLE_0=1
gen SAMPLE_1=(woven==1)
gen SAMPLE_2=(selected_p==1)
gen SAMPLE_3=(selected_p==1 & no_UDs==0)
gen SAMPLE_4=(selected_p==1 & no_UDs==0 & S_pd==1)
gen SAMPLE_5=(S_pds==1) 

label var ln_p_tr  "\${p}_{sbji}\$" 

* ---------------------------------------------------------------------------- *
* TABLE D4. Prices in Transactions and Orders Across Samples
* ---------------------------------------------------------------------------- *

do "${floats}\TableD4.do"
   
* ---------------------------------------------------------------------------- *
* FIGURE D1. Sourcing in Excluded and Included Products
* ---------------------------------------------------------------------------- *

do "${floats}\FigureD1.do"  
   

